package com.cloudxingkong.dianyinke.Student.Fragment;


import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.cloudxingkong.dianyinke.NewTeacher.Fragment.BaseFragment;
import com.cloudxingkong.dianyinke.R;
import com.cloudxingkong.dianyinke.Student.Adapter.KeMuListViewAdapter;
import com.cloudxingkong.dianyinke.Student.bean.KeMuTest;
import com.cloudxingkong.dianyinke.Uri.studentUri.StudentUri;
import com.cloudxingkong.dianyinke.Uri.teacherUri.ThisUri;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 *科目Fragment
 */
public class KeMuFragment extends BaseFragment implements KeMuListViewAdapter.getGridviewPosition {
    private List<KeMuTest.ListBean> lists;
    private ListView listView;
    private KeMuListViewAdapter adapter;
    private GetAdapterPosition getAdapterPosition;
    private View view;
    private SharedPreferences sharedPreferences;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_ke_mu, container, false);
        init();
        return view;
    }

    private void dataLoad() {
        x.http().get(new RequestParams(ThisUri.SERVER_YUMING + StudentUri.STUDENT_SUBJECT),
                new Callback.CommonCallback<String>() {
                    @Override
                    public void onSuccess(String result) {
                        Log.e("test", result);
                        KeMuTest keMuTest = (new Gson()).fromJson(result, KeMuTest.class);
                        lists.addAll(keMuTest.getList());
                        adapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {

                    }

                    @Override
                    public void onCancelled(CancelledException cex) {

                    }

                    @Override
                    public void onFinished() {

                    }
                });
    }
    private void init() {

        listView = (ListView) view.findViewById(R.id.lv_kemu);
        lists = new ArrayList<>();
        adapter = new KeMuListViewAdapter(lists, getActivity(), getFragmentManager(), this);
        listView.setAdapter(adapter);
    }
    /**
     * 实现Adapter中的回调接口
     * @param position 点击 GridView的position
     */
    @Override
    public void getPosition(int position,String pname) {

//        Log.e("test", "科目  kemu " + pname);

//        Bundle bundle = new Bundle();
//        bundle.putString("kemu", pname);
//        try {
//        keMuFragment.setArguments(bundle);
//        } catch (Exception e) {
//            Log.e("test", "--------- " + e.getMessage());
//        }
         sharedPreferences.edit().commit();
         sharedPreferences.edit().putString("kemu",pname).commit();
         getAdapterPosition.getPosition(position,pname);
    }
    /**
     * 准备回调到Activity上的本Fragment接口
     *
     */
    public interface GetAdapterPosition{
        public void getPosition(int position,String pname);
    }

    /**
     * 初始化
     * @param activity 确定应用到本接口的activity
     */
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        getAdapterPosition= (GetAdapterPosition) activity;
    }
    /**
     * 随着activity的销毁而销毁的fragment
     * 其中的接口也随之设置为null
     */
    @Override
    public void onDetach() {
        super.onDetach();
        getAdapterPosition=null;
    }
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            sharedPreferences = getActivity().getSharedPreferences("data_dyk", 0);

            dataLoad();
        }else{
            return;
        }
    }
}
